import logger from '../core/logger';
import areaRoomList from '../model/live/areaRoomList';
import liveHistoryItemModel from '../model/live/liveHistoryItemModel';
import liveMedalModel from '../model/live/liveMedalModel';
import liveTitleModel from '../model/live/liveTitleModel';
import liveUserInfoModel from '../model/live/liveUserInfoModel';
import livingModel from '../model/live/livingModel';
import notLivingModel from '../model/live/notLivingModel';
import returnModel from '../model/common/returnModel';
import apiInfo from './core/apiInfo';
import { clientBase } from './core/clientBase';

export class liveCenterClient  extends clientBase {
  constructor() {
    super();
  }

  cancelMedal(): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo('https://api.live.bilibili.com/xlive/web-room/v1/fansMedal/take_off', '', null, true);
        this.client.postResult(info,`csrf=&csrf_token=`).then((res) => {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>(res));
          else
            resolve(self.buildFailedResult<any>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<any>(e));
      }
    });
  }

  wearMedal(id: number): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo('https://api.live.bilibili.com/xlive/web-room/v1/fansMedal/wear', '', null, true);
        this.client.postResult(info, `medal_id=${id}`).then((res) => {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>(res));
          else
            resolve(self.buildFailedResult<any>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<any>(e));
      }
    });
  }

  getUserTitle(): Promise<returnModel<liveTitleModel>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo('https://api.live.bilibili.com/appUser/myTitleList', '&actionKey=appkey&scale=hdpi', null, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: liveTitleModel = res;
            resolve(self.buildSuccessResult<liveTitleModel>(data));
          } else
            resolve(self.buildFailedResult<liveTitleModel>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<liveTitleModel>(e));
      }
    });
  }

  getMedals(): Promise<returnModel<liveMedalModel>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo('https://api.live.bilibili.com/AppUser/medal', '&actionKey=appkey', null, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: liveMedalModel = res;
            resolve(self.buildSuccessResult<liveMedalModel>(data));
          } else
            resolve(self.buildFailedResult<liveMedalModel>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<liveMedalModel>(e));
      }
    });
  }

  getLiveList(): Promise<returnModel<Array<livingModel>>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo('https://api.live.bilibili.com/xlive/app-interface/v1/relation/liveAnchor', '&actionKey=appkey&qn=0&sortRule=0&filterRule=0', null, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: Array<livingModel> = res.data.rooms;
            resolve(self.buildSuccessResult<Array<livingModel>>(data));
          } else
            resolve(self.buildFailedResult<Array<livingModel>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<Array<livingModel>>(e));
      }
    });
  }

  getUserInfo(): Promise<returnModel<liveUserInfoModel>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo('https://api.live.bilibili.com/mobile/getUser', '&actionKey=appkey', null, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: liveUserInfoModel = res.data;
            resolve(self.buildSuccessResult<liveUserInfoModel>(data));
          } else
            resolve(self.buildFailedResult<liveUserInfoModel>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<liveUserInfoModel>(e));
      }
    });
  }

  getUnLiveList(page: number): Promise<returnModel<Array<notLivingModel>>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo('https://api.live.bilibili.com/xlive/app-interface/v1/relation/unliveAnchor', `&actionKey=appkey&page=${page}&pagesize=20`, null, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: Array<notLivingModel> = res.data.rooms;
            resolve(self.buildSuccessResult<Array<notLivingModel>>(data));
          } else
            resolve(self.buildFailedResult<Array<notLivingModel>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<Array<notLivingModel>>(e));
      }
    });
  }

  getHistory(pageIndex: number): Promise<returnModel<Array<liveHistoryItemModel>>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo('https://app.bilibili.com/x/v2/history/liveList', `&actionKey=appkey&scale=hdpi&pn=${pageIndex}&ps=20`, null, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: Array<liveHistoryItemModel> = res.data;
            resolve(self.buildSuccessResult<Array<liveHistoryItemModel>>(data));
          } else
            resolve(self.buildFailedResult<Array<liveHistoryItemModel>>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<Array<liveHistoryItemModel>>(e));
      }
    });
  }

  getRoomList(area_id: string, parent_area_id: string, page: number, sort_type: string = "online"): Promise<returnModel<areaRoomList>> {
    var self = this;
    return new Promise((resolve) => {
      try {
        let info = new apiInfo('https://api.live.bilibili.com/room/v3/Area/getRoomList', `actionKey=appkey&area_id=${area_id}&cate_id=0&page=${page}&page_size=20&parent_area_id=${parent_area_id}&qn=0&tag_version=1&sort_type=${sort_type}`, null, true);
        this.client.getResult(info).then((res) => {
          if (res.code == 0) {
            let data: areaRoomList = res.data;
            resolve(self.buildSuccessResult<areaRoomList>(data));
          } else
            resolve(self.buildFailedResult<areaRoomList>(res.message));
        });
      } catch (e) {
        logger.error(e);
        resolve(super.buildExceptionResult<areaRoomList>(e));
      }
    });
  }
}
export default new liveCenterClient();